﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Golf.WebUI.AdminShow.AttributeManagement
{
    public partial class Golf_AttributeZSG : Golf.WebUI.App_code.ValidateLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTypeName();
                switch (Request.Params["action"])
                {
                    case "modify"://修改
                        ShowInfo();
                        break;
                    case "add":
                        _Add();
                        break;
                    case "del":
                        _Del();
                        break;
                }
            }
        }
        /// <summary>
        /// 绑定父类数据
        /// </summary>
        public void BindTypeName()
        {
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            DropDownList1.DataSource = bll.GetList("AttrParent is not NULL and AttrChild is null");
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem() { Value = "0", Text = "我是父类" });
        }

        /// <summary>
        /// 删除
        /// </summary>
        private void _Del()
        {
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int Id = (Convert.ToInt32(Request.Params["id"]));
                if (bll.Delete(Id))
                {
                    Response.Write("成功删除,");
                }
                else
                {
                    Response.Write("删除失败,");
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void _Add()
        {
            _action.Value = "add";
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            lblID.Text = bll.GetMaxId() + 1 + "";

        }
        private void ShowInfo()
        {
            _action.Value = "modify";
            int ID = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                ID = (Convert.ToInt32(Request.Params["id"]));
            }
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            Golf.Model.Golf_Attribute model = bll.GetModel(ID);
            this.lblID.Text = model.ID.ToString();
            //this.txtParentID.Text = model.ParentID.ToString();
            try
            {
                if (model.ID == model.ParentID)
                {
                    DropDownList1.Items.FindByValue("0").Selected = true;
                }
                else
                {
                    DropDownList1.Items.FindByValue(model.ParentID.ToString()).Selected = true;
                }
            }
            catch (Exception)
            {
            }
            this.txtAttrParent.Text = model.AttrParent;
            this.txtAttrChild.Text = model.AttrChild;
            this.txtRemark.Text = model.Remark;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            int ParentID = 0;
            if (!PageValidate.IsNumber(DropDownList1.SelectedValue))
            {
                strErr += "子类的父类编号格式错误！\\n";
            }
            try
            {
                ParentID = int.Parse(DropDownList1.SelectedValue);//得到父类的id
            }
            catch (Exception)
            {
            }
            if (ParentID != 0)
                if (this.txtAttrParent.Text.Trim().Length == 0)
                {
                    strErr += "属性父类名称不能为空！\\n";
                }
                else
                    if (this.txtAttrChild.Text.Trim().Length == 0)
                    {
                        strErr += "属性子类名称不能为空！\\n";
                    }
            if (this.txtRemark.Text.Trim().Length == 0)
            {
                this.txtRemark.Text = null;
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int ID = int.Parse(this.lblID.Text);

            string AttrParent = this.txtAttrParent.Text;
            string AttrChild = this.txtAttrChild.Text;
            string Remark = this.txtRemark.Text;
            //如果是父类  子类名称为NULL
            if (ParentID == 0)
            {
                ParentID = int.Parse(lblID.Text);//设置父类id为ID
                AttrChild = null;
            }
            else
            {
                //不是父类  父类名称为NULL
                AttrParent = null;
            }

            Golf.Model.Golf_Attribute model = new Golf.Model.Golf_Attribute();
            model.ID = ID;
            model.ParentID = ParentID;
            model.AttrParent = AttrParent;
            model.AttrChild = AttrChild;
            model.Remark = Remark;

            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            bll.Update(model);
            if (_action.Value == "add")
            {
                if (bll.Add(model))
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('添加成功！');", true);
                }
                else
                {

                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('保存失败');", true);
                }
            }
            if (_action.Value == "modify")
            {
                if (bll.Update(model))
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('修改成功！');", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('修改失败');", true);
                }
            }
        }

        public void btnCancle_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('修改失败！');", true);
        }
    }
}
